﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// A player
    /// </summary>
    public class PlayerViewModel : ViewModelBase
    {
        /// <summary>
        /// The scores for this player
        /// </summary>
        public ObservableCollection<ResultViewModel> Results { get; private set; }

        /// <summary>
        /// The seasons (stats) for this player
        /// </summary>
        public ObservableCollection<PlayerSeasonViewModel> Seasons { get; private set; }

        /// <summary>
        /// Command to invoke when this player is selected
        /// </summary>
        public RelayCommand PlayerSelected { get; private set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="playerModel">Source data</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public PlayerViewModel(PlayerModel playerModel, INotifySelection notifySelection)
        {
            Results = new ObservableCollection<ResultViewModel>();
            Seasons = new ObservableCollection<PlayerSeasonViewModel>();
            PlayerSelected = new RelayCommand(p => notifySelection.PlayerSelected(playerModel)); 
            
            LastName = playerModel.LastName;
            FirstName = playerModel.FirstName;
            Gender = playerModel.Gender;
            GraduationYear = playerModel.GraduationYear;
            Hometown = playerModel.Hometown;

            // copy in the results for this player
            foreach (var resultModel in playerModel.Results)
            {
                Results.Add(new ResultViewModel(resultModel, notifySelection));
            }

            // build the seasons (stats) for this player
            PlayerSeasonViewModel playerSeasonViewModel = null;
            foreach (var resultModel in
                from resultModel in playerModel.Results
                orderby resultModel.TournamentModel.Season descending, resultModel.Group
                select resultModel)
            {
                if (playerSeasonViewModel == null ||
                    playerSeasonViewModel.Season.CompareTo(resultModel.TournamentModel.Season) != 0 ||
                    playerSeasonViewModel.Group.CompareTo(resultModel.Group) != 0)
                {
                    playerSeasonViewModel = new PlayerSeasonViewModel { Season = resultModel.TournamentModel.Season, Group = resultModel.Group };
                    Seasons.Add(playerSeasonViewModel);
                }

                playerSeasonViewModel.AddResult(resultModel);
            }
        }

        String _lastName;
        public String LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    Notify("LastName");
                }
            }
        }

        String _firstName;
        public String FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    Notify("FirstName");
                }
            }
        }

        String _gender;
        public String Gender
        {
            get
            {
                return _gender;
            }
            set
            {
                if (_gender != value)
                {
                    _gender = value;
                    Notify("Gender");
                }
            }
        }

        String _graduationYear;
        public String GraduationYear
        {
            get
            {
                return _graduationYear;
            }
            set
            {
                if (_graduationYear != value)
                {
                    _graduationYear = value;
                    Notify("GraduationYear");
                }
            }
        }

        String _hometown;
        public String Hometown
        {
            get
            {
                return _hometown;
            }
            set
            {
                if (_hometown != value)
                {
                    _hometown = value;
                    Notify("Hometown");
                }
            }
        }
    }
}
